--
-- Author: shunguo.chen
-- Date: 2018-11-19 11:42:39
--
-- TeamTargetSecondRender 组队查找器二级菜单
--

local ClassRef = app.mvc.createRenderClass(...)

local COLOR_SELECTED = cc.c3b( 255, 225, 132 )
local COLOR_UNSELECTED = cc.c3b( 97, 97, 97 )

--data = TeamTargetVo
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.TeamTargetNode_2))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui.Panel:getContentSize())
    self._ui.Panel:setTouchEnabled(true)
    self._ui.selected_btn:setTouchEnabled(false)
    display.uiAddClick(self._ui.Panel, function()
        executeFunc(self._onClick, self, self:isSelected(), self._data)
    end)

    self._iconRank = self._ui["Panel/icon_rank"] -- 竞速奖励icon
    self._iconRank:setVisible( false )

    self._ui.name:getVirtualRenderer():setLineSpacing(-5)

    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        local data = self._data
        self._ui.name:setString(data:getName())
        self._ui.Image:loadTexture(data:getMenuIcon())

        local isOpen = data:isOpen()
        local isSpeed = data:isSpeed()
        self._iconRank:setVisible(isSpeed and isOpen)
        self._ui.icon_lock:setVisible(not isOpen)

        local selectedBtnNormalPath -- 选中按钮正常态资源
        local selectedBtnBrightPath -- 选中按钮高亮态资源
        if isSpeed then
            local speedInfo = data:getSpeedInfo()
            if isOpen then
                self._iconRank:loadTexture(speedInfo:getRankIcon(), 1)
            end
            selectedBtnNormalPath = Res.commonUIPath .. "common_team_listbtn_2_n.png"
            selectedBtnBrightPath = Res.commonUIPath .. "common_team_listbtn_2_p.png"
        else
            selectedBtnNormalPath = Res.teamUIPath .. "team_list_map_n.png"
            selectedBtnBrightPath = Res.teamUIPath .. "team_list_map_p.png"
        end
        self._ui.selected_btn:loadTextures(selectedBtnNormalPath, selectedBtnBrightPath, selectedBtnBrightPath, 1)

        self:refreshAutoRecruit()
        self:refreshAutoApply()
        self:refreshInCrossMatch()
    end
end

function ClassRef:setSelected(b)
    self._ui.selected_btn:setBright(not b)

    local hasSub = #self._data.curSubTargets > 0
    self._ui.img_1:setVisible(hasSub and b)
    self._ui.img_2:setVisible(hasSub and not b)

    local color = b and COLOR_SELECTED or COLOR_UNSELECTED
    self._ui.name:setTextColor(color)
end

function ClassRef:isSelected()
    return not self._ui.selected_btn:isBright()
end

-- 自动招募标记，已经有队伍可能出现，只有一个
function ClassRef:refreshAutoRecruit()
    self._ui.icon_1:setVisible(self._data:isAutoRecruit(true))
end

-- 自动申请标记，没有队伍时可能出现，可以有多个
function ClassRef:refreshAutoApply()
    self._ui.icon_2:setVisible(self._data:isAutoApply(true))
end

-- 跨服匹配
function ClassRef:refreshInCrossMatch()
    self._ui.icon_3:setVisible(self._data:isInCrossMatch(true))
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
